from tkinter import Frame, filedialog

import pyperclip

from CommonOperations.parse_json import create_json_demo_file, batch_create_imgs_from_json
from CommonOperations.parse_xlsx import create_xlsx_demo, batch_create_imgs_from_xlsx
from CommonOperations.parse_xml import batch_create_imgs_from_xml, create_xml_demo_file
from CustomWidget.custom_button import CustomButtonPair, CustomLabelCopyButtonPair
from CustomWidget.custom_separator import CustomSeparator


class CreateImgsWithFile(Frame):
    """批量生成图片"""
    def __init__(self, master):
        self.root = master
        self.root.config()
        self.root.title("批量生成图片")
        self.root.geometry(f"500x400+200+200")

        super(CreateImgsWithFile, self).__init__(master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        json_demo = self.copy_json_demo_path
        json_file = self.find_json_file
        self.json_create = CustomButtonPair(self.root, 1, "复制json示例模板路径", json_demo, "选择json配置文件", json_file)
        self.json_imgs_path = CustomLabelCopyButtonPair(self.root, 2, "复制路径", left_text="")
        CustomSeparator(self.root, 4)

        xml_demo = self.copy_xml_demo_path
        xml_file = self.find_xml_file
        self.xml_create = CustomButtonPair(self.root, 6, "复制xml示例模板路径", xml_demo, "选择xml配置文件", xml_file)
        self.xml_imgs_path = CustomLabelCopyButtonPair(self.root, 7, "复制路径", left_text="")
        CustomSeparator(self.root, 9)

        xlsx_demo = self.copy_xlsx_demo_path
        xlsx_file = self.find_xlsx_file
        self.xlsx_create = CustomButtonPair(self.root, 11, "复制xlsx示例模板路径", xlsx_demo, "选择xlsx配置文件", xlsx_file)
        self.xlsx_imgs_path = CustomLabelCopyButtonPair(self.root, 12, "复制路径", left_text="")
        CustomSeparator(self.root, 14)

    def copy_xlsx_demo_path(self):
        """复制xlsx模板路径到系统剪切板"""
        xlsx_demo_path = create_xlsx_demo()
        pyperclip.copy(xlsx_demo_path)

    def find_xlsx_file(self):
        """选择xlsx配置文件"""
        choose_xlsx_file = filedialog.askopenfilename()

        if choose_xlsx_file != '':
            xlsx_batch_imgs_dir = batch_create_imgs_from_xlsx(choose_xlsx_file)
        try:
            self.xlsx_imgs_path.left_label['text'] = xlsx_batch_imgs_dir
        except:
            self.xlsx_imgs_path.left_label['text'] = ""

    def copy_xml_demo_path(self):
        """复制xml模板路径到系统剪切板"""
        xml_demo_path = create_xml_demo_file()
        pyperclip.copy(xml_demo_path)

    def find_xml_file(self):
        """选择xml配置文件"""
        choose_xml_file = filedialog.askopenfilename()

        if choose_xml_file != '':
            xml_batch_imgs_dir = batch_create_imgs_from_xml(choose_xml_file)
        try:
            self.xml_imgs_path.left_label['text'] = xml_batch_imgs_dir
        except:
            self.xml_imgs_path.left_label['text'] = ""

    def find_json_file(self):
        """选择json配置文件"""
        choose_json_file = filedialog.askopenfilename()

        if choose_json_file != '':
            json_batch_imgs_dir = batch_create_imgs_from_json(choose_json_file)
        try:
            self.json_imgs_path.left_label['text'] = json_batch_imgs_dir
        except:
            self.json_imgs_path.left_label['text'] = ""

    def copy_json_demo_path(self):
        """复制json模板路径到系统剪切板"""
        # 根据本地配置文件生成json模板
        json_demo_path = create_json_demo_file()
        pyperclip.copy(json_demo_path)
